package com.iflytek.iot.common.monitor.handler;

import com.iflytek.iot.common.monitor.model.MonitorTask;
import com.iflytek.iot.common.monitor.MonitorCallback;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * @author zurun
 * @date 2020/7/14 09:34:00
 */
public class MonitorContent {
    private static final Map<String, MonitorCallback> MAPS;
    private static final BlockingQueue<MonitorTask> QUEUE = new ArrayBlockingQueue<>(1_0000);

    static {
        MAPS = new ConcurrentHashMap<>(16);
    }

    /**
     * 增加监控注册事件
     *
     * @param monitorContent
     */
    public static Consumer<MonitorTask> add(MonitorCallback monitorContent) {
        MAPS.put(monitorContent.getName().getMonitorName(), monitorContent);
        return MonitorContent::addTask;
    }

    /**
     * 添加任务,非阻塞方法
     *
     * @param task
     * @return 添加成功返回true, 队列已满返回false
     */
    static boolean addTask(MonitorTask task) {
        return QUEUE.offer(task);
    }

    public static MonitorTask take() throws InterruptedException {
        return QUEUE.take();
    }
}